//
//  DriverAddInviteVC.m
//  LogisticsSupplier
//
//  Created by 吕晶 on 2021/3/24.
//  Copyright © 2021 吕晶. All rights reserved.
//

#import "DriverAddInviteVC.h"

@interface DriverAddInviteVC ()

@property (nonatomic, strong) UITextField *nameField;//司机姓名
@property (nonatomic, strong) UILabel *line3;
@property (nonatomic, strong) UITextField *emailField;//邮箱
@property (nonatomic, strong) UILabel *line4;
@property (nonatomic, strong) UITextField *phoneField;//电话
@property (nonatomic, strong) UILabel *line5;
@property (nonatomic, strong) UITextField *phoneCodeField;//验证码
@property (nonatomic, strong) UIButton *getCodeBtn;
@property (nonatomic, strong) UILabel *line6;
@property (nonatomic, strong) UIButton *commitBtn;

@end

@implementation DriverAddInviteVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"邀请司机注册";
    self.view.backgroundColor = HexColor(0xffffff);
    [self configUI];
}

- (void)configUI {
    
    UILabel *title3Lab = [[UILabel alloc] initWithFrame:CGRectMake(40, 60, 200, 15)];
    title3Lab.text = @"司机姓名";
    title3Lab.font = [UIFont systemFontOfSize:14];
    title3Lab.textColor = HexColor(0x2B2C44);
    [self.view addSubview:title3Lab];
    
    self.nameField = [[UITextField alloc] initWithFrame:CGRectMake(40, 10 + title3Lab.nim_bottom, ScreenW - 80, 40)];
    self.nameField.placeholder = @"必填，请输入司机姓名";
    self.nameField.font = MediumFontOfSize(16);
    self.nameField.textColor = HexColor(0x2B2C44);
    self.nameField.keyboardType = UIKeyboardTypeDefault;
    [self.view addSubview:self.nameField];
    
    self.line3 = [[UILabel alloc] initWithFrame:CGRectMake(40, self.nameField.nim_bottom + 10, ScreenW - 80, 1)];
    self.line3.backgroundColor = HexColor(0xF2F2F5);
    [self.view addSubview:self.line3];
    
    UILabel *title4Lab = [[UILabel alloc] initWithFrame:CGRectMake(40, self.line3.nim_bottom + 30, 200, 15)];
    title4Lab.text = @"司机邮箱";
    title4Lab.font = [UIFont systemFontOfSize:14];
    title4Lab.textColor = HexColor(0x2B2C44);
    [self.view addSubview:title4Lab];
    
    self.emailField = [[UITextField alloc] initWithFrame:CGRectMake(40, 10 + title4Lab.nim_bottom, ScreenW - 80, 40)];
    self.emailField.placeholder = @"请输入电子邮件地址，非必填项";
    self.emailField.font = MediumFontOfSize(16);
    self.emailField.textColor = HexColor(0x2B2C44);
    self.emailField.keyboardType = UIKeyboardTypeDefault;
    [self.view addSubview:self.emailField];
    
    self.line4 = [[UILabel alloc] initWithFrame:CGRectMake(40, self.emailField.nim_bottom + 10, ScreenW - 80, 1)];
    self.line4.backgroundColor = HexColor(0xF2F2F5);
    [self.view addSubview:self.line4];
    
    UILabel *title5Lab = [[UILabel alloc] initWithFrame:CGRectMake(40, self.line4.nim_bottom + 30, 200, 15)];
    title5Lab.text = @"司机电话";
    title5Lab.font = [UIFont systemFontOfSize:14];
    title5Lab.textColor = HexColor(0x2B2C44);
    [self.view addSubview:title5Lab];
    
    self.phoneField = [[UITextField alloc] initWithFrame:CGRectMake(40, 10 + title5Lab.nim_bottom, ScreenW - 80, 40)];
    self.phoneField.placeholder = @"请输入手机号，必填项";
    self.phoneField.font = MediumFontOfSize(16);
    self.phoneField.textColor = HexColor(0x2B2C44);
    self.phoneField.keyboardType = UIKeyboardTypeNumberPad;
    [self.view addSubview:self.phoneField];
    
    self.line5 = [[UILabel alloc] initWithFrame:CGRectMake(40, self.phoneField.nim_bottom + 10, ScreenW - 80, 1)];
    self.line5.backgroundColor = HexColor(0xF2F2F5);
    [self.view addSubview:self.line5];
    
    UILabel *title6Lab = [[UILabel alloc] initWithFrame:CGRectMake(40, self.line5.nim_bottom + 30, 200, 15)];
    title6Lab.text = @"验证码";
    title6Lab.font = [UIFont systemFontOfSize:14];
    title6Lab.textColor = HexColor(0x2B2C44);
    [self.view addSubview:title6Lab];
    
    self.phoneCodeField = [[UITextField alloc] initWithFrame:CGRectMake(40, 10 + title6Lab.nim_bottom, ScreenW - 80, 40)];
    self.phoneCodeField.placeholder = @"请输入";
    self.phoneCodeField.font = MediumFontOfSize(16);
    self.phoneCodeField.textColor = HexColor(0x2B2C44);
    self.phoneCodeField.keyboardType = UIKeyboardTypeNumberPad;
    [self.view addSubview:self.phoneCodeField];
    
    UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 102, 40)];
    self.getCodeBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 5, 102, 30)];
    self.getCodeBtn.layer.cornerRadius = 15;
    [self.getCodeBtn setBackgroundColor:HexColorA(0x3D68EB, 0.1)];
    [self.getCodeBtn setTitleColor:HexColor(0x3D68EB) forState:0];
    [self.getCodeBtn setTitle:@"获取验证码" forState:0];
    self.getCodeBtn.titleLabel.font = BoldFontOfSize(14);
    [self.getCodeBtn addTarget:self action:@selector(getCodeClick) forControlEvents:UIControlEventTouchUpInside];
    [rightView addSubview:self.getCodeBtn];
    self.phoneCodeField.rightView = rightView;
    self.phoneCodeField.rightViewMode = UITextFieldViewModeAlways;
    
    self.line6 = [[UILabel alloc] initWithFrame:CGRectMake(40, self.phoneCodeField.nim_bottom + 10, ScreenW - 80, 1)];
    self.line6.backgroundColor = HexColor(0xF2F2F5);
    [self.view addSubview:self.line6];
    
    self.commitBtn = [[UIButton alloc] initWithFrame:CGRectMake(40, self.line6.nim_bottom + 40, ScreenW - 80, 44)];
    [self.commitBtn setTitle:@"发送邀请" forState:0];
    [self.commitBtn setTitleColor:HexColor(0xffffff) forState:0];
    self.commitBtn.titleLabel.font = BoldFontOfSize(16);
    [self.commitBtn setBackgroundColor:HexColor(0x3D68EB)];
    self.commitBtn.layer.masksToBounds = YES;
    self.commitBtn.layer.cornerRadius = 6;
    [self.commitBtn addTarget:self action:@selector(commitClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.commitBtn];
}

- (void)commitClick {
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    if ([FactoryMethod isNull:self.nameField.text]) {
        [MBProgressHUD leShowFailedHUD:self.view message:@"请输入司机姓名"];
        return;
    }else {
        [dic setObject:self.nameField.text forKey:@"name"];
    }
    if ([FactoryMethod isNull:self.phoneField.text]) {
        [MBProgressHUD leShowFailedHUD:self.view message:@"请输入司机电话"];
        return;
    }
    if (self.emailField.text.length) {
        [dic setObject:self.emailField.text forKey:@"email"];
    }
    if ([FactoryMethod isNull:self.phoneField.text]) {
        [MBProgressHUD leShowFailedHUD:self.view message:@"请输入手机号"];
        return;
    }
    if (![FactoryMethod isPhone:self.phoneField.text]) {
        [MBProgressHUD leShowFailedHUD:self.view message:@"请输入正确的手机号"];
        return;
    }else {
        [dic setObject:self.phoneField.text forKey:@"phone"];
    }
    if ([FactoryMethod isNull:self.phoneCodeField.text]) {
        [MBProgressHUD leShowFailedHUD:self.view message:@"请输入验证码"];
        return;
    }else {
        [dic setObject:self.phoneCodeField.text forKey:@"code"];
    }
    [MBProgressHUD leShowHUDWithMessage:@"提交中" activity:YES];

    [HttpRequest PostWithApi:LogisticsSupplierDriverAddInvite_V_1_0 params:dic progress:^(NSProgress * _Nonnull progress) {
            
    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            __weak_self;
            [MBProgressHUD leShowSuccessHUDAddedTo:self.view message:@"已发送" delayOperation:^{
                [weakself.rt_navigationController popViewControllerAnimated:YES];
            }];
        }
    } fail:^(NSError * _Nonnull error) {
        
    }];
}

- (void)getCodeClick {
//    if ([FactoryMethod isNull:self.phoneField.text]) {
//        [MBProgressHUD leShowFailedHUD:self.view message:@"请输入手机号"];
//        return;
//    }
//    if (![FactoryMethod isPhone:self.phoneField.text]) {
//        [MBProgressHUD leShowFailedHUD:self.view message:@"请输入正确的手机号"];
//        return;
//    }
    [self timeStart];
    [self requestForGetCode];
}

- (void)timeStart{
    [self.phoneCodeField becomeFirstResponder];
    self.getCodeBtn.userInteractionEnabled = NO;
    self.phoneCodeField.text = @"";
    __block int timeout = 60; //倒计时时间
    /**
     *  gcd异步处理倒计时控件
     */
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout<=0){ //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                self.getCodeBtn.userInteractionEnabled = YES;
                [self.getCodeBtn setTitle:@"重新获取" forState:UIControlStateNormal];
                [self.getCodeBtn setTitleColor:HexColor(0x3D68EB) forState:UIControlStateNormal];
            });
        }else{
            int seconds = timeout;
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                self.getCodeBtn.userInteractionEnabled = NO;
                NSString *strTime = [NSString stringWithFormat:@"%d S",seconds];
                [self.getCodeBtn setTitle:strTime forState:(UIControlStateNormal)];
                [self.getCodeBtn setTitleColor:HexColor(0x2B2C44) forState:UIControlStateNormal];
            });
            timeout--;
        }
    });
    dispatch_resume(_timer);
}

- (void)requestForGetCode {
    [MBProgressHUD leShowHUDWithMessage:@"请求中" activity:YES];
    [HttpRequest PostWithApi:LogisticsSupplierDriverAddInviteGetCode_V_1_0 params:@{} progress:^(NSProgress * _Nonnull progress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            [MBProgressHUD leShowSuccessHUDAddedTo:self.view message:@"验证码获取成功"];
        }
    } fail:^(NSError * _Nonnull error) {
        
    }];
}

@end
